import 'package:cbec_app/entity/home/content/text_title_entity.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DecorationTitleText extends StatelessWidget {
  final TextTitleEntity info;

  const DecorationTitleText({super.key, required this.info});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: info.templateType == '0' ? _normalStyle() : _weChatStyle(),
    );
  }

  Widget _normalStyle() {
    return Container(
      color: Tools.fromHex(info.bgColor ?? ''),
      child: Row(
        mainAxisAlignment: _alignType(),
        children: [_buildChildrenBasedOnType()],
      ),
    );
  }

  Widget _weChatStyle() {
    return Row(
      children: [_buildWeChatChildrenBasedOnType()],
    );
  }

  Widget _titleStyle() {
    return Text(info.title ?? '',
        style: TextStyle(
          fontSize: double.parse(info.titleSize ?? ''),
          fontWeight: Tools.getWeight(info.titleWeight ?? ''),
          color: Tools.fromHex(info.titleColor ?? ''),
        ));
  }

  Widget _descStyle() {
    return Text(info.desc ?? '',
        style: TextStyle(
          fontSize: double.parse(info.descSize ?? ''),
          fontWeight: Tools.getWeight(info.descWeight ?? ''),
          color: Tools.fromHex(info.descColor ?? ''),
        ));
  }

  Widget _moreStyle() {
    final text = info.templateType == '0' ? info.linkText ?? '' : info.wxLinkTitle ?? '';
    return Visibility(
      visible: info.isLink == '1',
      child: InkWell(
        onTap: () {
          Tools.jumpPage(info.toJson());
        },
        child: Text(text,
            style: TextStyle(
              fontSize: double.parse(info.descSize ?? ''),
              fontWeight: Tools.getWeight(info.descWeight ?? ''),
              color: Colors.blue[700],
            )),
      ),
    );
  }

  MainAxisAlignment _alignType() {
    switch (info.align) {
      case '0':
        return MainAxisAlignment.start;
      case '1':
        return MainAxisAlignment.center;
      case '2':
        return MainAxisAlignment.end;
      default:
        return MainAxisAlignment.start;
    }
  }

  Widget _buildChildrenBasedOnType() {
    switch (info.align) {
      case '0':
        return Container(
          padding: EdgeInsets.all(15.w),
          width: ScreenUtil().screenWidth,
          color: Tools.fromHex(info.bgColor ?? ''),
          child: Stack(children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    _titleStyle(),
                    _moreStyle(),
                  ],
                ),
                _descStyle(),
              ],
            ),
            Positioned(bottom: 0, child: Container(height: 1.h, color: ThemeConfig.divideColor))
          ]),
        );
      case '1':
        return Container(
          // color: Colors.white,
          color: Tools.fromHex(info.bgColor ?? ''),
          padding: EdgeInsets.all(15.w),
          child: Column(
            children: [
              _titleStyle(),
              SizedBox(height: 10.w),
              _descStyle(),
              SizedBox(height: 10.w),
              _moreStyle(),
            ],
          ),
        );
      case '2':
        return Container(
          // color: Colors.white,
          color: Tools.fromHex(info.bgColor ?? ''),
          padding: EdgeInsets.all(15.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Row(
                children: [
                  _titleStyle(),
                  SizedBox(width: 20.w),
                  _moreStyle(),
                ],
              ),
              SizedBox(height: 10.w),
              _descStyle(),
            ],
          ),
        );
      default:
        return Container(
          padding: EdgeInsets.all(15.w),
          color: Tools.fromHex(info.bgColor ?? ''),
          width: ScreenUtil().screenWidth,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _titleStyle(),
                  _moreStyle(),
                ],
              ),
              _descStyle(),
            ],
          ),
        );
    }
  }

  Widget _buildWeChatChildrenBasedOnType() {
    switch (info.align) {
      case '0':
        return Container(
          padding: EdgeInsets.all(15.w),
          width: ScreenUtil().screenWidth,
          color: Colors.white,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _titleStyle(),
              SizedBox(height: 10.w),
              Row(
                children: [
                  if (info.wxDate == '' && info.wxAuthor == '') ...[
                    const SizedBox()
                  ] else ...[
                    _dateStyle(),
                    SizedBox(width: 10.w),
                    _authorStyle(),
                  ]
                ],
              ),
              SizedBox(height: 10.w),
              _moreStyle(),
            ],
          ),
        );
      case '1':
        return Container(
          // color: Colors.white,
          padding: EdgeInsets.all(15.w),
          width: ScreenUtil().screenWidth,
          child: Column(
            children: [
              _titleStyle(),
              SizedBox(height: 10.w),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (info.wxDate == '' && info.wxAuthor == '') ...[
                    const SizedBox()
                  ] else ...[
                    _dateStyle(),
                    SizedBox(width: 10.w),
                    _authorStyle(),
                  ]
                ],
              ),
              SizedBox(height: 10.w),
              _moreStyle(),
            ],
          ),
        );
      case '2':
        return Container(
          // color: Colors.white,
          padding: EdgeInsets.all(15.w),
          width: ScreenUtil().screenWidth,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              _titleStyle(),
              SizedBox(height: 10.w),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  if (info.wxDate == '' && info.wxAuthor == '') ...[
                    const SizedBox()
                  ] else ...[
                    _dateStyle(),
                    SizedBox(width: 10.w),
                    _authorStyle(),
                  ]
                ],
              ),
              SizedBox(height: 10.w),
              _moreStyle(),
            ],
          ),
        );
      default:
        return Container(
          padding: EdgeInsets.all(10.w),
          width: ScreenUtil().screenWidth,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              _titleStyle(),
              SizedBox(height: 10.w),
              Row(
                children: [
                  _dateStyle(),
                  _authorStyle(),
                ],
              ),
              SizedBox(height: 10.w),
              _moreStyle(),
            ],
          ),
        );
    }
  }

  Widget _dateStyle() {
    return Text(info.wxDate ?? '',
        style: TextStyle(
          fontSize: double.parse(info.descSize ?? ''),
          fontWeight: Tools.getWeight(info.descWeight ?? ''),
          color: Tools.fromHex(info.descColor ?? ''),
        ));
  }

  Widget _authorStyle() {
    return Text(info.wxAuthor ?? '',
        style: TextStyle(
          fontSize: double.parse(info.descSize ?? ''),
          fontWeight: Tools.getWeight(info.descWeight ?? ''),
          color: Tools.fromHex(info.descColor ?? ''),
        ));
  }
}
